﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BobcatMultiTouch;

namespace CNShapeEditor
{
    /// <summary>
    /// This gesture handler detects that you want to start a multi-touch zoom
    /// of the picture. It is triggered by seeing two input sources.
    /// </summary><remarks>
    /// To detect this gesture, the main method we need to implement from the
    /// GestureHandler base class in onTouchDown. We check whether we see
    /// two inputs and if we do, we trigger the new action.
    /// 
    /// Note: This action temporarily replaces the current active action with a 
    ///       zoom and then revertd to the previous action when it is finished.
    ///       Because of this we need to be careful not to trigger the gesture 
    ///       while the zoom action itself is still active, as this can lead to
    ///       it 'sticking' and not reverting to the previous action. To do this
    ///       we wait for the onNewAction event before starting to check for the
    ///       gesture again.
    /// </remarks>
    public class GestureHandler_MultiTouchZoomPicture : GestureHandler
    {
        #region Public methods

        public override void initialize()
        {
        }

        /// <summary>
        /// Called when a new action starts. We start checking for the gesture.
        /// </summary>
        public override void onNewAction()
        {
            m_bGestureCompleted = false;
        }

        /// <summary>
        /// Called when we get a touch-down event.
        /// </summary>
        public override void onTouchDown(TouchArgs touchArgs)
        {
            if (m_bGestureCompleted == true)
            {
                return;
            }

            // We check if there are two inputs...
            IList<TouchArgs> listTouches = m_ShapeEditorControl.MTIC.getActiveTouches();
            if (listTouches.Count != 2)
            {
                return;
            }

            // There are two inputs, so we want to start a zoom action...
            // We create and initialize the new action...
            Action_MultiTouchZoomPicture action = new Action_MultiTouchZoomPicture();
            action.initialize(m_Page, m_ShapeEditorControl);

            // We simulate the initial touch-down (as the touch-down has already happenend
            // as part of this gesture)...
            action.onTouchDown(touchArgs);

            // We create the args for the event we're raising...
            Args args = new Args();
            args.NewAction = action;
            args.RevertToPreviousActionWhenComplete = true;
            raiseGestureDetectedEvent(args);

            m_bGestureCompleted = true;
        }

        public override void onTouchMove(TouchArgs touchArgs)
        {
        }

        public override void onTouchUp(TouchArgs touchArgs)
        {
        }

        #endregion

        #region Private data

        private bool m_bGestureCompleted = false;

        #endregion
    }
}
